package com.tarena.lbs.marketing.web.controller;

import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.marketing.web.service.UserCouponsService;
import com.tarena.lbs.marketing.web.utils.AuthenticationContextUtils;
import com.tarena.lbs.pojo.marketing.param.UserCouponsParam;
import com.tarena.lbs.pojo.marketing.query.UserCouponCodeQuery;
import com.tarena.lbs.pojo.marketing.query.UserCouponQuery;
import com.tarena.lbs.pojo.marketing.vo.UserCouponsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;

/**
 * 处理的是用户 领取优惠券相关的功能
 * 领取记录
 * 查询领取记录
 */
@RestController
@Slf4j
public class UserCouponsController {
    @Autowired
    private UserCouponsService userCouponsService;
    @PostMapping("/marketing/user/receive/save")
    public Result<Void> receiveCoupon(@RequestBody UserCouponsParam param)
            throws BusinessException, ExecutionException, InterruptedException {
        long start=System.currentTimeMillis();
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        Asserts.isTrue(userPrinciple==null,new BusinessException("-2","认证解析失败"));
        param.setUserId(userPrinciple.getId());
        log.info("用户领取优惠券，入参：{}",param);
        //调用业务层 领取
        userCouponsService.receiveCoupon(param);
        long end=System.currentTimeMillis();
        log.info("当前领取优惠券业务执行时间:{}MS",end-start);
        return Result.success();
    }
    //查询我的优惠券列表
    @PostMapping("/marketing/user/receive/list")
    public Result<PageResult<UserCouponsVO>> userCouponList(@RequestBody UserCouponQuery couponQuery)
        throws BusinessException{
        return new Result<>(userCouponsService.userCouponList(couponQuery));
    }
    @PostMapping("/marketing/user/receive/detail")
    public Result<UserCouponsVO> userCouponsDetail(@RequestBody UserCouponCodeQuery query)
        throws BusinessException{
        return new Result<>(userCouponsService.userCouponsDetail(query));
    }
}
